import axios from 'axios'
// import { Message } from 'element-ui'
import store from '@/store'
// import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  (config) => {
    const { token } = store.state.user
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }

    return config
  }
)

// response interceptor
service.interceptors.response.use(
  (response) => {
    // console.log(response)
    // if (response.status === 400) {
    //   this.$router.path('/login')
    //   return Promise.reject(new Error('登录错误'))
    // }
    return response
  }

)

export default service
